package com.xm.xm.undercover.ui

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.xm.xm.undercover.MainActivity
import com.xm.xm.undercover.R
import com.xm.xm.undercover.bean.GetMessage
import com.xm.xm.undercover.bean.LogUser
import com.xm.xm.undercover.service.UserService
import com.xm.xm.undercover.utils.MessageUtils
import com.xm.xm.undercover.utils.ServiceCreator
import com.xm.xm.undercover.utils.UserInfo
import kotlinx.android.synthetic.main.activity_login.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class LoginActivity : AppCompatActivity() {

    private var account_id = ""
    private var password = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        bt_log_reg.setOnClickListener {
            val intent = Intent(this,RegUserActivity::class.java)
            startActivity(intent)
        }
        bt_play.setOnClickListener {
            val ans = login(this)
            Log.d("testActivity","ans : "+ans)
        }
    }

    private fun clearInfo(){
        log_name.text.clear()
        log_pass.text.clear()
    }

    private fun login(activity: Activity){
        account_id = log_name.text.toString()
        password = log_pass.text.toString()
        Log.d("testActivity","account : $account_id  password : $password")
        if (account_id.length>0&&password.length>0) {
            val user = LogUser(account_id, password)
            val userService = ServiceCreator.create<UserService>()
            val ans_user = userService.findUser(user)
            val res_user = ans_user.enqueue(object:Callback<GetMessage>{
                override fun onFailure(call: Call<GetMessage>, t: Throwable) {
                    t.printStackTrace()
                    Log.d("testActivity","fail")
                }

                override fun onResponse(call: Call<GetMessage>, response: Response<GetMessage>) {
                    val message = response.body()
                    if (message!=null){
                        Log.d("testActivity","message not null")
                        val mesId = MessageUtils.analyzeMes(message,this@LoginActivity)
                        Log.d("textActivity","mesid = "+mesId)
                        if (mesId.equals("100")){
                            //跳转到主页
                            Log.d("testActivity","to  main")
                            val intent = Intent(activity,MainActivity::class.java)
                            // 记录当前登录的用户id
                            UserInfo.curUserId = account_id
                            startActivity(intent)

                        }
                        else{
                            clearInfo()
                        }
                    }
                }
            })
        }
        else{
            Toast.makeText(this,"请输入完整的信息",Toast.LENGTH_SHORT).show()
        }
    }

}
